﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.StudentGroup")]
    public partial class StudentGroup : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private int _idStudentGroup;

        private short _refGroup;

        private short _refStudent;

        private EntityRef<Group> _Group;

        private EntityRef<Student> _Student;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnidStudentGroupChanging(int value);
        partial void OnidStudentGroupChanged();
        partial void OnrefGroupChanging(short value);
        partial void OnrefGroupChanged();
        partial void OnrefStudentChanging(short value);
        partial void OnrefStudentChanged();
        #endregion

        public StudentGroup()
        {
            this._Group = default(EntityRef<Group>);
            this._Student = default(EntityRef<Student>);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idStudentGroup", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public int idStudentGroup
        {
            get
            {
                return this._idStudentGroup;
            }
            set
            {
                if ((this._idStudentGroup != value))
                {
                    this.OnidStudentGroupChanging(value);
                    this.SendPropertyChanging();
                    this._idStudentGroup = value;
                    this.SendPropertyChanged("idStudentGroup");
                    this.OnidStudentGroupChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refGroup", DbType = "SmallInt NOT NULL")]
        public short refGroup
        {
            get
            {
                return this._refGroup;
            }
            set
            {
                if ((this._refGroup != value))
                {
                    if (this._Group.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefGroupChanging(value);
                    this.SendPropertyChanging();
                    this._refGroup = value;
                    this.SendPropertyChanged("refGroup");
                    this.OnrefGroupChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refStudent", DbType = "SmallInt NOT NULL")]
        public short refStudent
        {
            get
            {
                return this._refStudent;
            }
            set
            {
                if ((this._refStudent != value))
                {
                    if (this._Student.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefStudentChanging(value);
                    this.SendPropertyChanging();
                    this._refStudent = value;
                    this.SendPropertyChanged("refStudent");
                    this.OnrefStudentChanged();
                }
            }
        }

        [AssociationAttribute(Name = "Group_StudentGroup", Storage = "_Group", ThisKey = "refGroup", OtherKey = "idGroup", IsForeignKey = true)]
        public Group Group
        {
            get
            {
                return this._Group.Entity;
            }
            set
            {
                Group previousValue = this._Group.Entity;
                if (((previousValue != value)
                     || (this._Group.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Group.Entity = null;
                        previousValue.StudentGroups.Remove(this);
                    }
                    this._Group.Entity = value;
                    if ((value != null))
                    {
                        value.StudentGroups.Add(this);
                        this._refGroup = value.idGroup;
                    }
                    else
                    {
                        this._refGroup = default(short);
                    }
                    this.SendPropertyChanged("Group");
                }
            }
        }

        [AssociationAttribute(Name = "Student_StudentGroup", Storage = "_Student", ThisKey = "refStudent", OtherKey = "idStudent", IsForeignKey = true)]
        public Student Student
        {
            get
            {
                return this._Student.Entity;
            }
            set
            {
                Student previousValue = this._Student.Entity;
                if (((previousValue != value)
                     || (this._Student.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Student.Entity = null;
                        previousValue.StudentGroups.Remove(this);
                    }
                    this._Student.Entity = value;
                    if ((value != null))
                    {
                        value.StudentGroups.Add(this);
                        this._refStudent = value.idStudent;
                    }
                    else
                    {
                        this._refStudent = default(short);
                    }
                    this.SendPropertyChanged("Student");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}